package san.web.m;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import san.entity.m.Order;
import san.entity.m.Product;
import san.service.m.OrderService;
import san.service.m.ProductService;

import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

@ParentPackage("default")
@Results( { @Result(name = "reload", location = "order.shtml", type = "redirect") })
public class OrderAction extends CRUDSupportAction {
	/**
	 * 自动生成OrderAction类.
	 */
	private static final long serialVersionUID = 5500717274462774717L;
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductService productService;
	private Page<Order> page = new Page<Order>(10);// 每页10条记录
	private Order order;
	private List<Order> orders;
	private Page<Product> pageProduct = new Page<Product>(10);// 每页10条记录

	@Override
	protected void prepareModel() throws Exception {
		if (order != null && StringUtils.isNotBlank(order.getId())) {
			order = orderService.get(order.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				order = orderService.get(getId());
			} else {
				order = new Order();
			}
		}
	}

	@Override
	public String delete() throws Exception {
		if (StringUtils.isNotBlank(getId())) {
			orderService.delete(getId());
		}
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		return "input";
	}

	@Override
	public String list() throws Exception {
		setPage(orderService.getAll(page));
		return SUCCESS;
	}

	@SuppressWarnings("finally")
	@Override
	public String save() throws Exception {
		try {
			orderService.save(orders, order);
			addActionMessage("保存成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存失败!");
		} finally {
			return RELOAD;
		}
	}

	/**
	 * 在book()前执行二次绑定.
	 */
	public void prepareBook() throws Exception {
		prepareModel();
	}
	
	public String book() throws Exception {
		setPageProduct(productService.getAll(pageProduct));
		return "book";
	}

	/**
	 * 在saveOrder()前执行二次绑定.
	 */
	public void prepareSaveOrder() throws Exception {
		prepareModel();
	}
	
	@SuppressWarnings("finally")
	public String saveOrder() throws Exception {
		try {
			orderService.saveOrder(order);
			addActionMessage("保存成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存失败!");
		} finally {
			return RELOAD;
		}
	}

	public Page<Order> getPage() {
		return page;
	}

	public void setPage(Page<Order> page) {
		this.page = page;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public Page<Product> getPageProduct() {
		return pageProduct;
	}

	public void setPageProduct(Page<Product> pageProduct) {
		this.pageProduct = pageProduct;
	}

}
